package top.riske.animalhazardassessment.animal.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import top.riske.animalhazardassessment.animal.service.AnimalService;
import top.riske.animalhazardassessment.common.DTO.JwtDto;
import top.riske.animalhazardassessment.common.entity.Animal;
import top.riske.animalhazardassessment.common.enums.RoleEnum;
import top.riske.animalhazardassessment.common.http.exception.BusinessException;
import top.riske.animalhazardassessment.common.mapper.AnimalMapper;

import java.util.List;

@Service
public class AnimalServiceImpl extends ServiceImpl<AnimalMapper, Animal> implements AnimalService {

    @Resource
    private AnimalMapper animalMapper;

    @Override
    public Animal addAnimal(Animal animal) {
        animalMapper.insert(animal);
        return animal;
    }

    @Override
    public int deleteAnimal(Animal animal) {
        Animal  animal1 = animalMapper.selectById(animal.getId());
        if(animal1 == null) {
            throw new BusinessException("动物信息不存在");
        }

//        if(!animal1.getOwnerId().equals(jwtDto.getUid()) && !jwtDto.getRole().equals(RoleEnum.ADMIN)) {
//            throw new BusinessException("动物不是你的，你干嘛？");
//        }

        return animalMapper.deleteById(animal.getId());
    }

    @Override
    public int updateAnimal(Animal animal) {
        Animal  animal1 = animalMapper.selectById(animal.getId());
        if(animal1 == null) {
            throw new BusinessException("动物信息不存在");
        }

//        if(!animal1.getOwnerId().equals(jwtDto.getUid()) && !jwtDto.getRole().equals(RoleEnum.ADMIN)) {
//            throw new BusinessException("动物不是你的，你干嘛？");
//        }
        return animalMapper.updateById(animal);
    }

    @Override
    public List<Animal> getAnimals() {
        return animalMapper.selectList(null);
    }
}
